//向服务器端发送请求获取评论列表数据
$.ajax({
    type: 'get',
    url: '/comments',
    success: function(response) {
        console.log(response)
        var html = template('commentsTpl' , response)
        console.log(html);
        $('#commentsBox').html(html);
        var pageHTML = template('pageTpl', response)
        $('#pageBox').html(pageHTML);
    }
})

//实现分页
function changePage(page) {
    $.ajax({
        type: 'get',
        url:'/comments',
        data: {
            page: page
        },
        success: function(response) {
            var html = template('commentsTpl', response);
            $('#commentsBox').html(html);
            var pageHTML = template('pageTpl', response)
            $('#pageBox').html(pageHTML)
        }
    })
}

//当审核按钮被点击时
$('#commentsBox').on('click', '.status', function() {
    //获取当前评论的状态
    var status = $(this).attr('data-status');
    var id = $(this).attr('data-id');
    $.ajax({
        type: 'put',
        url: '/comments/' + id,
        data: {
            state: status == 0 ? 1 : 0
        },
        success: function() {
            location.reload();
            // console.log(id);
        }
    })
})

//当删除按钮被点击时
$('#commentsBox').on('click', '.delete', function() {
    if(confirm('确定要执行删除按钮吗')) {
        //获取id
        var id = $(this).attr('data-id');
        //向服务器端发送删除请求
        $.ajax({
            type: 'delete',
            url: '/comments/' + id,
            success: function() {
                location.reload();
            }
        })
    }
})